#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validates that RPMs are built with the correct prefix.
#
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2013 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id      "30.1"
set tar_name     "slurm-0-0.tar.bz2"
set prefix_loc   "_prefix /opt/slurm"
set rpm_loc      "/opt/slurm"
set topdir       "_topdir /tmp/built_rpms"
set man_dir      "_slurm_mandir /tmp/slurm_man/man"
set data_dir     "_slurm_datadir /tmp/slurm_data/data"
set lib_dir      "_libdir /opt/slurm/slurm_lib"
set bin_dir      "_bindir /etc/slurm"
set rpm_base     "/tmp/built_rpms/RPMS"
set file_name    "slurm-0-0"
set exit_code    0

print_header $test_id

proc check_path {path file_name} {
	global bin_bash bin_grep exit_code rpm_path

	set loc_match 0
	spawn $bin_bash -c "exec rpm -qpl $rpm_path | $bin_grep $file_name"
	expect {
		-re "$path" {
			set loc_match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: prefix was not enforced.\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$loc_match != 1} {
		send_user "\nFAILURE: $file_name was put in the wrong place\n"
		set exit_code 1
	}
}

if {[test_cray] || [test_alps]} {
	send_user "Warning: This test does not work correctly on a Cray (Native or ALPS) system\n";
	exit $exit_code
}

if { [catch {exec which rpmbuild}]} {
	send_user "Warning: This test requires rpmbuild to work.\n";
	exit $exit_code
}


set timeout 1200
exec $bin_rm -fr /tmp/slurm-0-0 $tar_name /tmp/built_rpms
spawn $bin_cp -L -R -v $src_dir /tmp/slurm-0-0
expect {
	timeout {
		send_user "\nFAILURE: $src_dir was not copied\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
exec $bin_rm -fr /tmp/slurm-0-0/.git
exec $bin_sed -i "s/Version:\t.*/Version:\t0/" /tmp/slurm-0-0/slurm.spec
exec $bin_sed -i "s/%global\ rel\t.*/%global\ rel\t0/" /tmp/slurm-0-0/slurm.spec
exec $bin_sed -i "s/Release:\t.*/Release:\t0/" /tmp/slurm-0-0/slurm.spec

set build_match 0
spawn tar -v -jcf $tar_name -C /tmp slurm-0-0
expect {
	-re "slurm/" {
		set build_match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: Slurm tar-ball create failed\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$build_match != 1} {
	send_user "\nFAILURE: did not tar src\n"
}

set build_match 0
spawn rpmbuild -ta -v $tar_name --define=$prefix_loc --define=$topdir --nodeps
expect {
	-re "Wrote" {
		set build_match 1
		exp_continue
	}
	-re "\nerror: db5 error" {
		send_user "\nWARNING: No installed RPM database on this system.\n"
		exp_continue
	}
	-re "\nerror: cannot open Packages" {
		send_user "\nWARNING: No installed RPM database on this system.\n"
		exp_continue
	}
	-re "\nerror: " {
		send_user "\nFAILURE: There was an error during the RPM build.\n"
		set exit_code 1
		exp_continue
	}
	-re "\nwarning: File listed twice: /etc/slurm/slurm.conf.example" {
		send_user "\nWARNING: This seems to be an rpmbuild problem.\n"
		exp_continue
	}
	-re "\nwarning: remember to run 'libtool --finish" {
		send_user "\nWARNING: This seems to be a harmless warning.\n"
		exp_continue
	}
	-re "\nwarning: relinking" {
		send_user "\nWARNING: This seems to be a harmless warning.\n"
		exp_continue
	}
	-re "\nwarning: Empty %files file" {
		send_user "\nWARNING: This seems to be a harmless warning.\n"
		exp_continue
	}
	-re "\nwarning: " {
		send_user "\nFAILURE: There was a warning during the RPM build.\n"
		set exit_code 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: rpmbuild is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$build_match != 1} {
	send_user "\nFAILURE: there was an error during the rpmbuild\n"
	set exit_code 1
}

# Determine RPM build path. It could include "x86_64", "i386", etc.
set sub_dir ""
set timeout 10
spawn ls $rpm_base
expect {
	-re "($alpha_numeric_under)" {
		set sub_dir $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}
set rpm_path "${rpm_base}/${sub_dir}/slurm-0-0.${sub_dir}.rpm"
set timeout 1200

# Check to see that srun man pages are in the correct directory.
check_path /opt/slurm srun.1

# Check to see that srun is in the correct directory.
check_path /opt/slurm srun

set rpm_path "${rpm_base}/${sub_dir}/slurm-slurmctld-0-0.${sub_dir}.rpm"
# Check to see that slurmctld is in the correct directory.
check_path /opt/slurm/sbin slurmctld

if {$exit_code == 0} {
	exec $bin_rm -fr /tmp/built_rpm
} else {
	send_user "\nFAILURE: In phase one of the rpmbuild\n"
	exit $exit_code
}

set build_match 0
spawn rpmbuild -ta -v $tar_name --define=$prefix_loc --define=$lib_dir \
    --define=$topdir --define=$man_dir --define=$data_dir --define=$bin_dir --nodeps
expect {
	-re "Wrote" {
		set build_match 1
		exp_continue
	}
	-re "\nerror: db5 error" {
		send_user "\nWARNING: No installed RPM database on this system.\n"
		exp_continue
	}
	-re "\nerror: cannot open Packages" {
		send_user "\nWARNING: No installed RPM database on this system.\n"
		exp_continue
	}
	-re "\nerror: " {
		send_user "\nFAILURE: There was an error during the RPM build.\n"
		set exit_code 1
		exp_continue
	}
	-re "\nwarning: File listed twice: /etc/slurm/slurm.conf.example" {
		send_user "\nWARNING: This seems to be an rpmbuild problem.\n"
		exp_continue
	}
	-re "\nwarning: Empty %files file" {
		send_user "\nWARNING: This seems to be a harmless warning problem.\n"
		exp_continue
	}
	-re "\nwarning: " {
		send_user "\nFAILURE: There was a warning during the RPM build.\n"
		set exit_code 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: rpmbuild is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$build_match != 1} {
	send_user "\nFAILURE: there was an error during the rpmbuild\n"
	set exit_code 1
}

set rpm_path "${rpm_base}/${sub_dir}/slurm-0-0.${sub_dir}.rpm"
# Check that scontrol is in the correct place
check_path /etc/slurm scontrol

# Check that srun man page is in the correct place
check_path /tmp/slurm_man/man srun.1

# Check that libraries are in the correct place
check_path /opt/slurm/slurm_lib lib

# Check that web pages are in the correct place
check_path /tmp/slurm_data/data accounting.html

set rpm_path "${rpm_base}/${sub_dir}/slurm-slurmctld-0-0.${sub_dir}.rpm"
# Check that slurmctld is in the correct place
check_path /opt/slurm/sbin slurmctld

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
	exec $bin_rm -fr /tmp/slurm-0-0 $tar_name /tmp/built_rpms
} else {
	send_user "\nFAILURE: In phase two of the rpmbuild\n"
}

exit $exit_code
